const mutations = {
  setCurrentUser (state, user) {
    state.currentUser = user
  },
  setCurrentUserInfo (state, userId) {
    let userInfoArr = state.userInfoArr
    if (userInfoArr === undefined) {
      userInfoArr = []
    }
    let i = 0
    for (; i < userInfoArr.length; i++) {
      if (userInfoArr[i].userId === userId) {
        break
      }
    }

    if (i === userInfoArr.length) { // 用户第一次登录，信息不存在
      let userInfo = {
        userId,
        conversations: [],
        friendRequestMsgArr: [],
        momentMsgArr: []
      }
      userInfoArr.push(userInfo)
      state.conversations = []
      state.friendRequestMsgArr = []
      state.momentMsgArr = []
    } else {
      state.conversations = userInfoArr[i].conversations
      state.friendRequestMsgArr = userInfoArr[i].friendRequestMsgArr
      state.momentMsgArr = userInfoArr[i].momentMsgArr
    }

    state.userInfoArr = userInfoArr
  },
  setUsers (state, users) {
    state.users = users
  },
  setSocket (state, socket) {
    state.socket = socket
  },
  setConversations (state, conversations) {
    state.conversations = conversations

    let userInfoArr = state.userInfoArr
    let userId = state.currentUser.id
    let i = 0
    for (; i < userInfoArr.length; i++) {
      if (userInfoArr[i].userId === userId) {
        break
      }
    }
    userInfoArr[i].conversations = conversations
    state.userInfoArr = userInfoArr
  },
  setSelectedConversation (state, selectedConversation) {
    state.selectedConversation = selectedConversation
  },
  updateConversation (state, selectedConversation) {
    let conversations = state.conversations
    let i = 0
    for (i = 0; i < conversations.length; i++) {
      if (conversations[i].id === selectedConversation.id) {
        break
      }
    }
    conversations[i] = selectedConversation
    state.conversations = conversations
    state.selectedConversation = selectedConversation
  },
  setUnreadMsgSum (state, unreadMsgSum) {
    state.unreadMsgSum = unreadMsgSum
  },
  setSelectedItem (state, selectedItem) {
    state.selectedItem = selectedItem
  },
  setMomentMsgSum (state, sum) {
    this.state.momentMsgSum = sum
  },
  addFriendRequestMsg (state, message) {
    let friendRequestMsgArr = this.state.friendRequestMsgArr
    if (friendRequestMsgArr === undefined) {
      friendRequestMsgArr = []
    }
    friendRequestMsgArr.unshift(message)

    this.state.friendRequestMsgArr = friendRequestMsgArr

    let userInfoArr = state.userInfoArr
    let userId = state.currentUser.id
    let i = 0
    for (; i < userInfoArr.length; i++) {
      if (userInfoArr[i].userId === userId) {
        break
      }
    }
    userInfoArr[i].friendRequestMsgArr = friendRequestMsgArr
  },
  setFriendRequestMsgArr (state, friendRequestMsgArr) {
    state.friendRequestMsgArr = friendRequestMsgArr

    let userInfoArr = state.userInfoArr
    let userId = state.currentUser.id
    let i = 0
    for (; i < userInfoArr.length; i++) {
      if (userInfoArr[i].userId === userId) {
        break
      }
    }
    userInfoArr[i].friendRequestMsgArr = friendRequestMsgArr
  },
  setMomentMsgArr (state, momentMsgArr) {
    state.momentMsgArr = momentMsgArr

    let userInfoArr = state.userInfoArr
    let userId = state.currentUser.id
    let i = 0
    for (; i < userInfoArr.length; i++) {
      if (userInfoArr[i].userId === userId) {
        break
      }
    }
    userInfoArr[i].momentMsgArr = momentMsgArr
  }
}

export default mutations
